


module.exports = {
	updateManager,

}

function updateManager() {
 
// #ifdef APP-PLUS

const updated = uni.getStorageSync('updated'); // 尝试读取storage
// 如果上次刚更新过----删除 updated成功标志
// console.log(updated)
if (updated.completed === true) {
	// 删除安装包及安装记录 
	
	console.log('安装记录被删除，更新成功');
	uni.removeSavedFile({
		filePath: updated.packgePath,
		success: res => {
			uni.removeStorageSync('updated');
		}
	});
} else if (updated.completed === false) {
	
	uni.removeStorageSync('updated');
	plus.runtime.install(updated.packgePath, {
		force: true
	});
	uni.setStorage({
		key: 'updated',
		data: {
			completed: true,
			packgePath: updated.packgePath
		},
		success: res => {
			console.log('成功安装上次的更新，应用需要重启才能继续完成');
		}
	});
	uni.showModal({
		title: '提示',
		content: '应用将重启以完成更新',
		showCancel: false,
		complete: () => {
			plus.runtime.restart();
		}
	});
}  

// #endif
 
//checkUpdater();
};
import CONFIG  from "./../config/global.config"
export  function checkUpdater(currentId, updaterPage) {
  
 
 
	let version=null
	plus.runtime.getProperty(plus.runtime.appid, (wgtinfo)=>{  
	  version=wgtinfo.versionCode; 
			console.log("当前版本"+version)	  
				 
				  uni.request({
					url: CONFIG.baseUrl+ "/app/config/info",
					method: 'get',
				 
					success:(res)=>{
					
						if (res.data.code != 200 ) {
							uni.showModal({
								title: '提示',
								content: res.data.msg
							})
							return;
						}
 
						
						let upgradeInfo = res.data.data;
						 
						  console.log("res"+JSON.stringify(upgradeInfo))
					 
						if (Number(version) <Number( upgradeInfo.number)) {      
					 
							uni.showModal({
								title: '发现新版本',
								content: '有新版本可用 (版本号:' + upgradeInfo.ver + ')，请问您是否更新？',
								showCancel:false,
								success: (res) => {
									if (res.confirm) {
										console.log(plus.os.name.toLowerCase())
										if (plus.os.name.toLowerCase() === 'ios') {
											let appleId=1538532925; //app的appleId id1538532925
											plus.runtime.launchApplication({
												action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
											}, function(e) {
												console.log('Open system default browser failed: ' + e.message);
											});
										} else {
											if(upgradeInfo.status){
												uni.redirectTo({
													url: '/pages/index/update',
													success(res) {
														console.log(res);
														},
														fail(err) {
														console.log(err);
														}
												})
											}else{
												uni.navigateTo({
													url: '/pages/index/update',
													success(res) {
														console.log(res);
														},
														fail(err) {
														console.log(err);
														}
												})
											}
											
										}
						
										//	downLoad();
									} else if (res.cancel) {
										console.log('取消')
									}
								},
								 
							})
						}
					} 
					
				});
			})  
				  
			 
 
	 
}